package com.bugsnag.ohos;

import com.bugsnag.ohos.internal.ImmutableConfig;

import java.io.IOException;

/**
 * Stateful information set by the notifier about your app can be found on this class. These values
 * can be accessed and amended if necessary.
 *
 * @since 2021-07-01
 */
public class AppWithState extends App {
    /**
     * The number of milliseconds the application was running before the event occurred
     */
    Number duration;

    /**
     * The number of milliseconds the application was running in the foreground before the
     * event occurred
     */
    Number durationInForeground;

    /**
     * Whether the application was in the foreground when the event occurred
     */
    boolean inForeground;

    /**
     * Whether the application was launching when the event occurred
     */
    boolean isLaunching;

    /**
     * constructor
     *
     * @param binaryArch
     * @param id
     * @param releaseStage
     * @param version
     * @param codeBundleId
     * @param buildUuid
     * @param type
     * @param versionCode
     * @param duration
     * @param durationInForeground
     * @param inForeground
     * @param isLaunching
     */
    public AppWithState(String binaryArch, String id, String releaseStage,
                        String version, String codeBundleId, String buildUuid,
                        String type, Number versionCode, Number duration,
                        Number durationInForeground, boolean inForeground, boolean isLaunching) {
        super(binaryArch, id, releaseStage, version, codeBundleId, buildUuid, type, versionCode);
        this.duration = duration;
        this.durationInForeground = durationInForeground;
        this.inForeground = inForeground;
        this.isLaunching = isLaunching;
    }

    /**
     * constructor
     *
     * @param config
     * @param binaryArch
     * @param id
     * @param releaseStage
     * @param version
     * @param codeBundleId
     * @param duration
     * @param durationInForeground
     * @param inForeground
     * @param isLaunching
     */
    public AppWithState(ImmutableConfig config, String binaryArch, String id,
                        String releaseStage, String version, String codeBundleId,
                        Number duration, Number durationInForeground,
                        boolean inForeground, boolean isLaunching) {
        this(binaryArch, id, releaseStage, version, codeBundleId, config.getBuildUuid(),
            config.getAppType(), config.getVersionCode(), duration, durationInForeground, inForeground, isLaunching);
    }

    public boolean isLaunching() {
        return isLaunching;
    }

    @Override
    public void serialiseFields(JsonStream writer) throws IOException {
        super.serialiseFields(writer);
        writer.name("duration").value(duration);
        writer.name("durationInForeground").value(durationInForeground);
        writer.name("inForeground").value(inForeground);
        writer.name("isLaunching").value(isLaunching);
    }

    public Number getDuration() {
        return duration;
    }

    public void setDuration(Number duration) {
        this.duration = duration;
    }

    public Number getDurationInForeground() {
        return durationInForeground;
    }

    public void setDurationInForeground(Number durationInForeground) {
        this.durationInForeground = durationInForeground;
    }

    public boolean getInForeground() {
        return inForeground;
    }

    public void setInForeground(boolean inForeground) {
        this.inForeground = inForeground;
    }

    public boolean getLaunching() {
        return isLaunching;
    }

    public void setLaunching(boolean launching) {
        isLaunching = launching;
    }
}
